# -*- coding: utf-8 -*-
# from dsp.filterButter import FilterButter
import numpy as np

from collections import deque
from scipy import signal

class Filters2(object):
    """docstring for FilterButter"""
    def __init__(self,fs,ch_num,order,fc,s): 
        # s is 'highpass' or 'lowpass' or .'notch'..
        # if use notch, the order would be QUALITY_FACTOR, and fc will be notch_freq

        super(Filters2, self).__init__()
        self.ch_num = ch_num
        nyquist = fs / 2

        if s == 'notch':
            self.b, self.a = signal.iirnotch(fc / nyquist, order)
        elif s == 'lowpass':
            self.b, self.a = signal.butter(order,(fc / nyquist),'lowpass')
        elif s == 'highpass':
            self.b, self.a = signal.butter(order,(fc / nyquist),'highpass')
        else:
            print('not defined, check this error!')

        self.zi =  [signal.lfilter_zi(self.b, self.a) for _ in range(self.ch_num)]

    def filter(self,arr):
        # arr should be 1d n*m matrix, for example, 10*32, means 10 time points and 32 channels
        d_len,ch_num = arr.shape
        # filtered_data = np.zeros(shape=(arr.shape))

        for ch in range(ch_num):
            arr[:,ch], self.zi[ch] = signal.lfilter(self.b, self.a, arr[:,ch], zi=self.zi[ch])
        return arr


class DSPx2(object):
    """docstring for DSPx"""
    def __init__(self,fs,ch_num):
        super(DSPx2, self).__init__()

        self.hp = Filters2(fs,ch_num,3,0.3,'highpass')
        self.lp = Filters2(fs,ch_num,3,40,'lowpass')
        self.notch = Filters2(fs,ch_num,10,50,'notch')

    def filter_dummy(self,arr):
        return arr

    def filter(self,arr):
        # arr should be 2 dimensional array, for example shape = (6,8), means 8 channels, 6 time points
        # return self.lp.filter(self.hp.filter(self.notch.filter(arr)))
        return self.hp.filter(self.notch.filter(arr))
        # return self.lp.filter(self.notch.filter(arr))
        # return self.notch.filter(arr)





